#include<iostream>
using namespace std;
#include<vector>
#include<queue>
class Solution {
public:
    vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {
        int m = mat.size(), n = mat[0].size();
        vector<vector<int>> ans(m, vector<int>(n));
        queue<pair<int, int>> que;
        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                if (mat[i][j] == 0) {
                    que.push({ i,j });
                }
            }
        }
        int dx[4] = { -1,1,0,0 }, dy[4] = { 0,0,-1,1 };
        int now = 0;
        while (que.size()) {
            int size = que.size();
            ++now;
            while (size--) {
                int x1 = que.front().first, y1 = que.front().second;
                que.pop();
                for (int i = 0; i < 4; ++i) {
                    int x = x1 + dx[i], y = y1 + dy[i];
                    if (x >= 0 && x < m && y >= 0 && y < n && mat[x][y] == 1 && ans[x][y] == 0) {
                        que.push({ x,y });
                        ans[x][y] = now;
                    }
                }
            }
        }
        return ans;
    }
};
int main() {
    vector<vector<int>> mat{ {0,0,0},{0,1,0},{0,0,0} };
    Solution().updateMatrix(mat);
}